/**

 @Name：layuiAdmin iframe版主入口
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */

// ie8 forEach 重构
if ( !Array.prototype.forEach ) {
    Array.prototype.forEach = function forEach( callback, thisArg ) {
        var T, k;
        if ( this == null ) {
            throw new TypeError( "this is null or not defined" );
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if ( typeof callback !== "function" ) {
            throw new TypeError( callback + " is not a function" );
        }
        if ( arguments.length > 1 ) {
            T = thisArg;
        }
        k = 0;
        while( k < len ) {
            var kValue;
            if ( k in O ) {

                kValue = O[ k ];
                callback.call( T, kValue, k, O );
            }
            k++;
        }
    };
}

// ie8 map 重构
if (!Array.prototype.map) {
    Array.prototype.map = function(callback, thisArg) {
        var T, A, k;
        if (this == null) {
            throw new TypeError(" this is null or not defined");
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if (typeof callback !== "function") {
            throw new TypeError(callback + " is not a function");
        }
        if (thisArg) {
            T = thisArg;
        }
        A = new Array(len);
        k = 0;
        while(k < len) {
            var kValue, mappedValue;
            if (k in O) {
                kValue = O[ k ];
                mappedValue = callback.call(T, kValue, k, O);
                A[ k ] = mappedValue;
            }
            k++;
        }
        return A;
    };
}
layui.extend({
    setter: 'config' //配置模块
    ,admin: 'lib/admin' //核心模块
    ,view: 'lib/view' //视图渲染模块
    ,jquerySetting:'lib/extend/jquerySetting'
    ,ajaxSetting:'lib/extend/ajaxSetting'
}).define(['setter', 'admin','initData','jquerySetting','ajaxSetting'], function(exports){
    var setter = layui.setter
        ,element = layui.element
        ,admin = layui.admin
        ,tabsPage = admin.tabsPage
        ,initData = layui.initData
        ,view = layui.view

        //打开标签页
        ,openTabsPage = function(url, text){
            //遍历页签选项卡
            var matchTo
                ,tabs = $('#LAY_app_tabsheader>li')
                ,path = url.replace(/(^http(s*):)|(\?[\s\S]*$)/g, '');

            tabs.each(function(index){
                var li = $(this)
                    ,layid = li.attr('lay-id');

                if(layid === url){
                    matchTo = true;
                    tabsPage.index = index;
                }
            });

            text = text || '新标签页';

            if(setter.pageTabs){
                //如果未在选项卡中匹配到，则追加选项卡
                if(!matchTo){
                    $(APP_BODY).append([
                        '<div class="layadmin-tabsbody-item layui-show">'
                        ,'<iframe src="'+ url +'" frameborder="0" class="layadmin-iframe"></iframe>'
                        ,'</div>'
                    ].join(''));
                    tabsPage.index = tabs.length;
                    element.tabAdd(FILTER_TAB_TBAS, {
                        title: '<span>'+ text +'</span>'
                        ,id: url
                        ,attr: path
                    });
                }
            } else {
                var iframe = admin.tabsBody(admin.tabsPage.index).find('.layadmin-iframe');
                iframe[0].contentWindow.location.href = url;
            }

            //定位当前tabs
            element.tabChange(FILTER_TAB_TBAS, url);
            admin.tabsBodyChange(tabsPage.index, {
                url: url
                ,text: text
            });
            if (localStorage.getItem('pageTabs') === '1'){
                admin.resizeTable(); //重置当前主体区域的表格尺寸
            }
        }

        ,APP_BODY = '#LAY_app_body', FILTER_TAB_TBAS = 'layadmin-layout-tabs'
        ,$ = layui.$, $win = $(window);

    //初始
    if(admin.screen() < 2) admin.sideFlexible();

    //将模块根路径设置为 controller 目录
    layui.config({
        base: setter.base + 'controller/'
    });


    //扩展 lib 目录下的其它模块
    layui.each(setter.extend, function(index, item){
        var mods = {};
        mods[item] = '{/}' + setter.base + 'lib/extend/' + item;
        layui.extend(mods);
    });

    view().autoRender();

    //加载公共模块
    layui.use('common');

    //对外输出
    exports('index', {
        openTabsPage: openTabsPage
    });
});
